package com.house.rent.ui.fragment;

import android.view.View;
import android.widget.TextView;

import com.base.lib.mvp.present.BasePresenter;
import com.base.lib.util.MaterialDialogUtils;
import com.base.lib.util.ScreenManager;
import com.house.rent.App;
import com.house.rent.R;
import com.house.rent.entity.UserEntity;
import com.house.rent.ui.activity.AboutUsActivity;
import com.house.rent.ui.activity.CheckIdCardActivity;
import com.house.rent.ui.activity.LoginActivity;
import com.house.rent.ui.activity.MainActivity;
import com.house.rent.ui.activity.ModifyPwdActivity;
import com.house.rent.ui.activity.MyCollectionActivity;
import com.house.rent.ui.activity.MyPublishActivity;
import com.house.rent.ui.activity.PersonInfoActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_user_nick)
    TextView mTvUserName;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected void initTop() {
        enableTop(false);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        UserEntity userEntity = App.getInstance().getUserEntity();
        mTvUserName.setText(userEntity.getNickName());

        if (userEntity.isAdmin()) {
            getView().findViewById(R.id.tv_user_check_idCard).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.line_user_check_idCard).setVisibility(View.VISIBLE);
        } else {
            getView().findViewById(R.id.tv_user_check_idCard).setVisibility(View.GONE);
            getView().findViewById(R.id.line_user_check_idCard).setVisibility(View.GONE);
        }
    }

    @Override
    protected void initListeners() {
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        //获取个人信息
        getUserInfo();
    }

    private void getUserInfo() {
        addParams("userId", App.getInstance().getUserEntity().getId());
        sendRequest("user/userInfo", UserEntity.class);
    }

    @Override
    public void showLoading() {
    }

    @Override
    protected void onRequestSuccess(String reqId, Object obj) {
        super.onRequestSuccess(reqId, obj);
        if (obj instanceof UserEntity) {
            App.getInstance().saveUserEntity((UserEntity) obj);
            initView();
        }
    }

    @OnClick({R.id.tv_user_info, R.id.tv_user_change_pwd, R.id.tv_user_publish,
            R.id.tv_user_collection, R.id.tv_user_check_idCard, R.id.tv_about_us,
            R.id.btn_logout})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.tv_user_info:
                PersonInfoActivity.start(mContext);
                break;
            case R.id.tv_user_change_pwd:
                ModifyPwdActivity.start(mContext);
                break;
            case R.id.tv_user_publish:
                MyPublishActivity.start(mContext);
                break;
            case R.id.tv_user_collection:
                MyCollectionActivity.start(mContext);
                break;
            case R.id.tv_user_check_idCard:
                CheckIdCardActivity.start(mContext);
                break;
            case R.id.tv_about_us:
                AboutUsActivity.start(mContext);
                break;
            case R.id.btn_logout:
                MaterialDialogUtils.show(mContext, "是否退出？",
                        (dialog, which) -> logout());
                break;
            default:
                break;
        }
    }

    /**
     * 退出登录操作
     */
    private void logout() {
        //清空session，本地登录状态
        App.getInstance().logout();
        ScreenManager.getInstance().popActivity(MainActivity.class);
        LoginActivity.start(mContext);
    }
}
